package com.lizemin.inputoutput.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.lizemin.inputoutput.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author lzm
 * @date 2025/4/4 9:28
 * @description
 */
@Slf4j
@RestController
public class TestController {

    @GetMapping("/heart")
    public void heart(HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write("heart".getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();
    }

    @GetMapping("/test")
    public void test(HttpServletResponse response) throws IOException {
        log.info("进入了接口中的测试方法");
//        throw new RuntimeException("测试异常");
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.print("test");
    }

    @PostMapping("/getRequestBody")
    public UserDTO getRequestBody(@RequestBody UserDTO user) {
        log.info("请求入参为：{}", JSONUtil.toJsonStr(user));
        return user;
    }


}
